#include "bluezwindow.h"
#include "ui_bluezwindow.h"

#include <KF5/BluezQt/bluezqt/devicesmodel.h>
#include <QSortFilterProxyModel>
#include "bluezmanager.h"
#include "devicedelegate.h"
#include "pairdialog.h"
struct BluezWindow::BluezWindowPrivate
{
    BluezQt::ManagerPtr manager;
    BluezManager* bzManager{nullptr};
    BluezQt::DevicesModel* devicesModel{nullptr};
    PairDialog* dialog{nullptr};
};

BluezWindow::BluezWindow(QWidget* parent)
    : QMainWindow(parent)
    , ui(new Ui::BluezWindow)
    , d_ptr(new BluezWindowPrivate)

{
    ui->setupUi(this);
    d_ptr->manager = BluezQt::ManagerPtr(new BluezQt::Manager());
    d_ptr->bzManager = new BluezManager(d_ptr->manager, this);
    d_ptr->dialog = new PairDialog(this);
    d_ptr->devicesModel = new BluezQt::DevicesModel(d_ptr->manager.data(), this);

    ui->devicelist->setModel(d_ptr->devicesModel);
    ui->devicelist->setItemDelegate(new DeviceDelegate());

    connect(ui->devicelist, &QListView::clicked, this, &BluezWindow::selectDevice);
    connect(ui->power, &QPushButton::clicked, d_ptr->bzManager, &BluezManager::clickPower);
    connect(d_ptr->bzManager, &BluezManager::setAdapterName, this, [this](const QString & name)
    {
        ui->label->setText(name);
    });
    connect(d_ptr->bzManager, &BluezManager::switchPower, this, &BluezWindow::switchPower);
}

BluezWindow::~BluezWindow()
{
    delete d_ptr;
    delete ui;
}

void BluezWindow::switchPower(bool flag)
{
    ui->power->setText(!flag ? "On" : "Off");
}

void BluezWindow::selectDevice(const QModelIndex& index)
{
    BluezQt::DevicePtr device = d_ptr->devicesModel->device(index);
    if(device)
    {
        if(device->isConnected() || device->isPaired())
        {
            int result = d_ptr->dialog->showConfirmation(0x08, device->name());
            if(result == QDialog::Accepted)
            {
                if(device->isConnected())
                {
                    device->disconnectFromDevice();
                }
                else
                {
                    d_ptr->bzManager->removeDevice(device);
                }
            }
        }
        else
        {
            d_ptr->bzManager->stopDiscovery();
            auto call = device->pair();
            connect(call, &BluezQt::PendingCall::finished, this, [this, device](BluezQt::PendingCall * callResult)
            {
                if(callResult->error())
                {
                    d_ptr->bzManager->startDiscovery();
                    qDebug("Pair err:%d", callResult->error());
                }
                else
                {
                    auto connCall = device->connectToDevice();
                    connect(connCall, &BluezQt::PendingCall::finished, this, [this](BluezQt::PendingCall * connCallResult)
                    {
                        d_ptr->bzManager->startDiscovery();
                        if(connCallResult->error())
                        {
                            qDebug("ConnectDevice Error:%d", connCallResult->error());
                        }
                        else
                        {
                            qDebug("devce connect finish");
                        }
                    });
                }
            });
            // d_ptr->deviceConnector->PushDevice(device);
        }
    }
}
